package com.wowudi.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wowudi.dto.ListDto;
import com.wowudi.entity.Patients;
import com.wowudi.entity.RegisterType;
import com.wowudi.service.*;
import com.wowudi.util.MsgUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Random;

    @RestController
    @RequestMapping("api/pati/pati")
    public class PatiController {
        @Autowired
        private PatientsService patientsService;
        @Autowired
        private TreatResultService treatResultService;
        @Autowired
        private RegisterTypeService registerTypeService;
        @Autowired
        private RegisterCostService registerCostService;
        @Autowired
        private DepartmentClassService departmentClassService;
        //分页查询
        @RequestMapping("findPati")
        public Page<Patients> findPati(Integer pageym ,Integer pagesize) {
           // List list = this.patientsService.list();
          //  System.out.println(pageDto.getPageSize()+"页数"+pageDto.getPage());
            Page<Patients> page = new Page<Patients>(pageym,pagesize);
            this.patientsService.getAll(page);
            return page;
        }
        //根据主键查询
        @RequestMapping("findPatiByid")
        public Page<Patients> findPat(Integer pageym ,Integer pagesize,String cardid,String patiname) {
            Page<Patients> page = new Page<Patients>(pageym,pagesize);
            this.patientsService.getAllBynameandid(page,cardid,patiname);
            return page;
        }

        //添加
        @RequestMapping("addPati")
        public MsgUtil addPati(@RequestBody Patients patients) {
            String str="16153";
            Random rand = new Random();
            int a=rand.nextInt(89999999)+10000000;
            patients.setPtCard(str+a);
            patients.setPtCreateTime(new Date());
            patients.setPtIsdel(0);
            patients.setTsId(1);
            boolean bool=this.patientsService.save(patients);
            if (bool){
                return new MsgUtil(true,"添加成功",null);
            }else {
                return new MsgUtil(false,"添加失败",null);
            }
        }
        //删除
        @RequestMapping("delPati")
        public MsgUtil delPati(Integer id) {
            Patients patients=this.patientsService.getById(id);
            patients.setPtIsdel(1);
           boolean bool=this.patientsService.updateById(patients);
            if (bool){
                    return new MsgUtil(true,"删除成功",null);
            }else {
                 return new MsgUtil(false, "删除失败", null);
            }
        }
        //显示修改ui
        @RequestMapping("upPatiui")
        public Patients upPatiui(Integer id) {
            Patients patients=this.patientsService.getById(id);
            return patients;
        }
        //修改
        @RequestMapping("upPati")
        public MsgUtil upPati(@RequestBody Patients patients) {
            boolean bool=this.patientsService.updateById(patients);
            if (bool){
                return new MsgUtil(true,"修改成功",null);
            }else {
                return new MsgUtil(false, "修改失败", null);
            }
        }

        @RequestMapping("getListDto")
        public ListDto getListDto() {
            ListDto listDto=new ListDto(this.registerTypeService.list(),this.registerCostService.list(),this.departmentClassService.list(),null,null);
            return listDto;
        }
    }
